package com.ellen.mac.chatdemo.adpter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.avos.avoscloud.im.v2.AVIMConversation;
import com.avos.avoscloud.im.v2.AVIMException;
import com.avos.avoscloud.im.v2.callback.AVIMConversationCreatedCallback;
import com.ellen.mac.chatdemo.activity.ChatRoomAct;
import com.ellen.mac.chatdemo.model.CustomUserProvider;
import com.ellen.mac.chatdemo.R;
import com.ellen.mac.chatdemo.comment.BaseApp;
import com.ellen.mac.chatdemo.model.ChatKitUser;
import com.ellen.mac.chatdemo.utils.ChatKit;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mac on 2017/12/2.
 */

public class TabFragmentAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> mFragments;
    private List<String> mTitles;
    private Context context;

    public TabFragmentAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles, Context context) {
        super(fm);
        mFragments = fragments;
        mTitles = titles;
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }


    private void gotoSquareConversation() {
        List<ChatKitUser> userList = CustomUserProvider.getInstance().getAllUsers();
        List<String> idList = new ArrayList<>();
        for (ChatKitUser user : userList) {
            idList.add(user.getUserId());
        }
        ChatKit.getInstance().getClient().createConversation(
                idList, context.getString(R.string.square), null, false, true, new AVIMConversationCreatedCallback() {
                    @Override
                    public void done(AVIMConversation avimConversation, AVIMException e) {
                        Intent intent = new Intent(context, ChatRoomAct.class);
                        intent.putExtra(BaseApp.CONVERSATION_ID, avimConversation.getConversationId());
                        context.startActivity(intent);
                    }
                });
    }
}
